package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserInfoSplit;
import com.koron.css2.clientCenter.bean.UserInfoSplitDetail;
import com.koron.css2.clientCenter.mapper.UserInfoSplitDetailMapper;
import com.koron.css2.clientCenter.mapper.UserInfoSplitMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoSplitDetailQuery;
import com.koron.css2.clientCenter.queryBean.UserInfoSplitQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BusinessAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

public class UserInfoSplitSingle implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoSplitQuery userInfoSplitQuery = JsonUtils.objectToPojo(req.getData(),UserInfoSplitQuery.class);
            UserInfoSplitMapper userInfoSplitMapper = factory.getMapper(UserInfoSplitMapper.class);
            UserInfoSplitDetailMapper userInfoSplitDetailMapper = factory.getMapper(UserInfoSplitDetailMapper.class);
            BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);

            Map<String, Map> areaNameIdMap = businessAreaMapper.getNameAndId();

            // 1. 查出对应的拆户数据
            UserInfoSplit userInfoSplit = userInfoSplitMapper.getSingle(userInfoSplitQuery);
            // 2. 查出对应的拆户详情
            UserInfoSplitDetailQuery userInfoSplitDetailQuery = new UserInfoSplitDetailQuery();
            userInfoSplitDetailQuery.setSplitCode(userInfoSplit.getBillNo());
            List<UserInfoSplitDetail> userInfoSplitDetailList = userInfoSplitDetailMapper.getList(userInfoSplitDetailQuery);
            userInfoSplitDetailList.forEach(userInfoSplitDetail -> {
                // 2.1businessArea->businessAreaName
                String businessArea = userInfoSplitDetail.getBusinessArea();
                if (StringUtils.isNotEmpty(businessArea)) {
                    userInfoSplitDetail.setBusinessAreaName(areaNameIdMap.get(businessArea).get("area_name").toString());
                }
            });
            // 3. 返回数据
            userInfoSplit.setUserInfoSplitDetailList(userInfoSplitDetailList);
            MessageBean<UserInfoSplit> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"拆户详情查询成功",UserInfoSplit.class);
            messageBean.setData(userInfoSplit);
            return messageBean;
        } catch (Exception e) {
            logger.error("拆户详情查询失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"拆户详情查询失败",null);
        }
    }
}
